package com.liush.srb.core.controller.api;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.liush.srb.base.result.ResponseVo;
import com.liush.srb.base.util.JwtUtils;
import com.liush.srb.core.pojo.entity.UserAccount;
import com.liush.srb.core.service.UserAccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Map;

/**
 * @Author: liush
 * @CreateTime: 2023-04-11  19:19
 * @Description: 用户账户 前端控制器
 */
@Api(tags = "用户账户模块")
@RestController
@RequestMapping("/api/core/userAccount")
public class ApiUserAccountController {

    @Resource
    UserAccountService userAccountService;

    @ApiOperation("构建自动提交的充值表单")
    // 表单自动提交的地址: http://localhost:9999/userAccount/AgreeBankCharge
    @PostMapping("charge")
    public ResponseVo charge(BigDecimal amount,
                             @RequestHeader(value = "token", required = false) String token) {
        String formStr = userAccountService.charge(amount, token);
        return ResponseVo.ok().data("form", formStr);
    }

    //http://192.168.1.171/api/core/userAccount/notify
    //@RequestParam Map<String,Object> map 表示解析请求参数存到map中
    // Map<String,Object> map  表示request域  和 Model model
    @ApiOperation("充值完成后的回调接口")
    @PostMapping("notify")
    public String rechargeNotify(@RequestParam Map<String, Object> map) {
        System.out.println("map = " + map);
        return userAccountService.rechargeNotify(map);
    }

    @ApiOperation("查询账户余额")
    @GetMapping("/amount")
    public ResponseVo amount(@RequestHeader(value = "token", required = false) String token) {
        Long userId = JwtUtils.getUserId(token);
        UserAccount userAccount = userAccountService.getOne(Wrappers.lambdaQuery(UserAccount.class)
                .eq(UserAccount::getUserId, userId)
                .select(UserAccount::getAmount));
        return ResponseVo.ok().data("amount", userAccount.getAmount());
    }
}
